package com.expense.app.admin.services.bmw.impl;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.expense.app.admin.common.BaseService;
import com.expense.app.admin.model.dto.bmw.bmwStudent.*;
import com.expense.app.admin.model.params.bmw.bmwStudent.*;
import com.expense.app.admin.services.bmw.BmwStudentServ;
import com.expense.common.constant.enums.DeletedStatus;
import com.expense.common.dao.bmw.BmwStudentDao;
import com.expense.common.exception.PlatformException;
import com.expense.common.model.auth.AdminUser;
import com.expense.common.model.bmw.BmwStudent;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
 * 宝马学生报名
 */
@Service
public class BmwStudentServImpl extends BaseService implements BmwStudentServ {
    private static final Log log = LogFactory.get();

    @Autowired
    BmwStudentDao bmwStudentDao;

    @Override
    public BmwStudentListDTO findList(BmwStudentListParam param) {
        Map<String,Object> condition = new HashMap<>();
        condition.put("key",param.getKey());
        PageQuery pageQuery = initPageQuery(param.getPage(),param.getSort());
        pageQuery.setParas(condition);
        bmwStudentDao.findListPage(pageQuery);
        return new BmwStudentListDTO(pageQuery.getList(),pageQuery.getTotalRow());
    }

    @Override
    public BmwStudent findOne(Long id) {
        return bmwStudentDao.findByIdAndDeleted(id);
    }

    @Override
    public BmwStudentDTO findDetail(Long id) {
        BmwStudent entity = bmwStudentDao.findByIdAndDeleted(id);
        return new BmwStudentDTO(entity);
    }

    @Override
    public BmwStudent saveOne(BmwStudentParam param) {
        BmwStudent entity = param.getEntity();
        setBaseInfo(entity);
        if(entity.getDeleted() == null){
            entity.setCreateAt(new Date());
            entity.setDeleted(DeletedStatus.UN_DELETED);
        }
        /*BmwStudent student = bmwStudentDao.findByTelAndEmail(entity.getTel(), entity.getSceneId());
        if(student != null){
            return entity;
        }*/
        bmwStudentDao.saveOne(entity);
        //其他关联处理
        return entity;
    }

    @Override
    public Long deleteOne(Long id) {
        if(0L != id){
            BmwStudent entity = bmwStudentDao.findByIdAndDeleted(id);
            if(entity == null) throw new PlatformException("宝马学生报名已不存在");
            entity.setDeleted(DeletedStatus.DELETED);
            setBaseInfo(entity);
            bmwStudentDao.updateTemplateById(entity);
        }else{
            AdminUser loginUser = getCurrentUser();
            bmwStudentDao.deleteAll(loginUser.getId());
        }

        //其他关联处理
        return id;
    }
}
